package com.zhx.threadcoreknowledge.threadobjectclasscommonmethod;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @author ：zhanghaixuan
 * @date ：Created in 2019-10-03 18:05
 * 用途         ： 使用 interrupt 可以打断 sleep  或者说 sleep可以响应 interrupt
 */
public class SleepInterrupted implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(new Date());

            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("我被中断了");
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new SleepInterrupted());
        thread.start();
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();
    }
}
